﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Collections;
using Telerik.Web.UI;
using System.Drawing;

namespace BVVD_Website.ql_danh_muc
{
    public partial class frmLophoc2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grvLophoc.DataSource = getData();
                grvLophoc.DataBind();

            }
        }
        protected void grvLophoc_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            grvLophoc.DataSource = getData();
        }

        private List<LST_LOPHOC> getData()
        {
            BVVDDataStore<LST_LOPHOC> db = new BVVDDataStore<LST_LOPHOC>();
            return db.getFromDB(new Hashtable());

        }
        protected void grvLophoc_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string recid = item.OwnerTableView.DataKeyValues[item.ItemIndex]["RECID"].ToString();
            BVVDDataContext db = new BVVDDataContext();
            LST_LOPHOC lophoc = (from dm in db.LST_LOPHOCs
                                 where dm.RECID.ToString().Equals(recid)
                                 select dm).SingleOrDefault();
            db.LST_LOPHOCs.DeleteOnSubmit(lophoc);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Xóa thành công lớp học có mã : " + lophoc.MALH;
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n " + ex.Message;
            }

            rnAlert.Show();
        }
        protected void grvLophoc_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
            LST_LOPHOC lophoc = new LST_LOPHOC
            {
                MALH = (insertedItem.FindControl("txtMALH") as RadTextBox).Text,
                TENLH = (insertedItem.FindControl("txtTENLH") as RadTextBox).Text,
                THOIGIANHOC = Convert.ToInt32((insertedItem.FindControl("txtTHOIGIANHOC") as RadNumericTextBox).Value),
                HOCPHI = Convert.ToInt32((insertedItem.FindControl("txtHOCPHI") as RadNumericTextBox).Value),
                GHICHU = (insertedItem.FindControl("txtGHICHU") as RadTextBox).Text,
                TRANGTHAI = (insertedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue
            };
            BVVDDataContext db = new BVVDDataContext();
            db.LST_LOPHOCs.InsertOnSubmit(lophoc);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Thêm mới lớp học thành công  ";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra \n" + ex.Message;

            }


            rnAlert.Show();
        }
        protected void grvLophoc_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {

            BVVDDataContext db = new BVVDDataContext();
            GridEditableItem editedItem = e.Item as GridEditableItem;
            string recid = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["RECID"].ToString();

            LST_LOPHOC lophoc = (from item in db.LST_LOPHOCs
                                 where item.RECID.ToString().Equals(recid)
                                 select item).SingleOrDefault();

            lophoc.TENLH = (editedItem.FindControl("txtTENLH") as RadTextBox).Text;
            lophoc.THOIGIANHOC = Convert.ToInt32((editedItem.FindControl("txtTHOIGIANHOC") as RadNumericTextBox).Value);
            lophoc.HOCPHI = Convert.ToInt32((editedItem.FindControl("txtHOCPHI") as RadNumericTextBox).Value);
            lophoc.GHICHU = (editedItem.FindControl("txtGHICHU") as RadTextBox).Text;
            lophoc.TRANGTHAI = (editedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue;
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Cập nhật thành công lớp học có mã : " + lophoc.MALH;
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n " + ex.Message;
            }

            rnAlert.Show();
        }
    }
}